package cn.xshi.oauth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.dao.OauthAccountTenantDao;
import cn.xshi.oauth.model.OauthAccountTenant;
import cn.xshi.oauth.param.OauthAccountTenantParam;
import cn.xshi.oauth.service.OauthAccountTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @Desc 账号租户关系
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
@Service
public class OauthAccountTenantServiceImpl extends BaseService implements OauthAccountTenantService {

    @Resource
    private OauthAccountTenantDao oauthAccountTenantDao;

    /**
     * 查询集合
     * @param oauthAccountTenantParam
     * @return
     */
    public List<OauthAccountTenant> getOauthAccountTenantList(OauthAccountTenantParam oauthAccountTenantParam){
        return oauthAccountTenantDao.getOauthAccountTenantList(oauthAccountTenantParam);
    }

    /**
     * 新增
     * @param oauthAccountTenantList
     * @param account
     * @return
     */
    public int addOauthAccountTenant(List<OauthAccountTenant> oauthAccountTenantList,String account){
        int i = 0;
        if(StringUtil.isEmpty(account)){
            return -1;
        }
//        oauthAccountTenantDao.delOauthAccountTenantByAccount(account);
        if(CollectionUtil.isNotEmpty(oauthAccountTenantList)){
            String userId = getCurrentAccountId();
            Date date = getDate();
            for(OauthAccountTenant oauthAccountTenant: oauthAccountTenantList){
                OauthAccountTenantParam oauthAccountTenantParam = new OauthAccountTenantParam();
                oauthAccountTenantParam.setAccount(account);
//                oauthAccountTenantParam.setTenantId(oauthAccountTenant.getTenantId());
                oauthAccountTenantParam.setSysModeId(oauthAccountTenant.getSysModeId());
                oauthAccountTenantParam.setAccountTypeId(oauthAccountTenant.getAccountTypeId());
                List<OauthAccountTenant> oauthAccountTenants = oauthAccountTenantDao.getOauthAccountTenantList(oauthAccountTenantParam);

                oauthAccountTenant.setId(toUUID());
                oauthAccountTenant.setCreateId(userId);
                oauthAccountTenant.setUpdateId(userId);
                oauthAccountTenant.setCreateTime(date);
                oauthAccountTenant.setUpdateTime(date);
                oauthAccountTenant.setAccount(account);
                if(CollectionUtil.isEmpty(oauthAccountTenants)){
                    i = oauthAccountTenantDao.addOauthAccountTenant(oauthAccountTenant);
                }else{
                    throw new ExceptionUtil("该账户下一个子系统只能有一个租户！（"+oauthAccountTenant.getSysModeId()+"）");
                }
            }
        }
        return i;
    }

    /**
     * 根据账号删除
     * @param account
     * @return
     */
    public int delOauthAccountTenantByAccount(String account){
        return oauthAccountTenantDao.delOauthAccountTenantByAccount(account);
    }

    /**
     * 删除
     * @param condition
     * @return
     */
    public int delOauthAccountTenant(Map<String,Object> condition){
        return oauthAccountTenantDao.delOauthAccountTenant(condition);
    }

    /**
     * 统计绑定租户
     * @param oauthAccountTenantParam
     * @return
     */
    public Integer getOauthAccountTenantCount(OauthAccountTenantParam oauthAccountTenantParam){
        return oauthAccountTenantDao.getOauthAccountTenantCount(oauthAccountTenantParam);
    }

    /**
     * 根据账号，类型删除
     * @param oauthAccountTenantParam
     * @return
     */
    public int delOauthAccountTenantByAccountAndType(OauthAccountTenantParam oauthAccountTenantParam){
        return oauthAccountTenantDao.delOauthAccountTenantByAccountAndType(oauthAccountTenantParam);
    }

    /**
     * 根据子系统删除
     * @param oauthAccountTenantParam
     * @return
     */
    public int delOauthAccountTenantBySysModeId(OauthAccountTenantParam oauthAccountTenantParam){
        return oauthAccountTenantDao.delOauthAccountTenantBySysModeId(oauthAccountTenantParam);
    }
}
